// pages/goods/goods.js
const { baseUrl } = require('../../globalData.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // url基础地址赋值
        baseUrl,
        // query相关
        currentPage: 1,
        pageSize: 8,
        sortTag: [
            { id: 1, html: '综合', isActive: true },
            { id: 2, html: '销量', isActive: false },
            { id: 3, html: '新品', isActive: false },
            {
                id: 4,
                html: '价格'
                , isActive: false
            },
            {
                id: 5,
                html: '筛选'
                , isActive: false
            }
        ],
        goodsList: [],
        // 当前的请求路径的索引
        currentRouteNum: 1
    },
    // 分类按钮
    goodsClazz() {

    },
    // 查询商品
    searchText() {
        wx.navigateTo({
            url: '/pages/search/index'
        })
    },
    // 切换tab数据列表
    tabList(e) {
        const num = e.currentTarget.dataset.num;
        console.log(num);
        this.setData({
            currentRouteNum: num
        })
        const list = this.data.sortTag
        list.forEach((item) => {
            item.isActive = false
            list[num - 1].isActive = true
        })
        this.setData({
            sortTag: list
        })
        this.setData({
            currentPage: 1,
            goodsList: []
        })
        switch (num) {
            case 1: this.goodsAllDataInit('/goodsListAll'); break;
            case 2: this.goodsAllDataInit('/goodsListSales'); break;
            case 3: this.goodsAllDataInit('/goodsListNew'); break;
            case 4: this.goodsAllDataInit('/goodsListPrice'); break;
            default: return false;
        }
    },
    // 请求对应条件的商品数据列表
    // 参数1：接口路径，带有/
    goodsAllDataInit(route) {
        wx.request({
            url: baseUrl + `${route}?currentPage=${this.data.currentPage}&pageSize=${this.data.pageSize}`,
            //	请求成功回调
            success: (res) => {
                let newList = res.data.data.rows
                let oldList = this.data.goodsList
                newList = oldList.concat(newList)
                this.setData({
                    goodsList: newList
                })
            },
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.goodsAllDataInit('/goodsListAll')
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        const num = this.data.currentRouteNum
        let newCurrent = this.data.currentPage
        newCurrent++
        this.setData({
            currentPage: newCurrent
        })
        switch (num) {
            case 1: this.goodsAllDataInit('/goodsListAll'); break;
            case 2: this.goodsAllDataInit('/goodsListSales'); break;
            case 3: this.goodsAllDataInit('/goodsListNew'); break;
            case 4: this.goodsAllDataInit('/goodsListPrice'); break;
            default: return false;
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})